﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class SupplierController : CommonController
    {
        public ActionResult Index(string search)
        {
            var data = EdexContext.GetSuppliers(search);

            foreach (var record in data)
            {
                record.MAP = EdexContext.GetCompanyMap(record.GUID_RECORD);
            }

            return View(data);
        }

        public ActionResult Details(Guid id)
        {
            var data = EdexContext.GetSupplierDetails(id);
            data.MAP = EdexContext.GetCompanyMap(data.GUID_RECORD);

            return View(data);
        }

        public ActionResult DetailsNew()
        {
            return View();
        }

        public ActionResult MapSave(Guid COMPANY_GUID, Guid SUPPLIER_GUID)
        {
            EdexContext.SaveCompanyMap(COMPANY_GUID, SUPPLIER_GUID);
            return Content("OK");
        }

        public ActionResult FindAndReturnHtmlList(string id)
        {
            var data = EdexContext.GetSuppliers(id);
            var result_string = String.Join(String.Empty, data.Select(r => String.Format("<option value='{0}'>{1}</option>", r.GUID_RECORD, r.COMPANY_NAME)));

            return Content(result_string);
        }

        public ActionResult Save(EdexApiClient.WH_COMPANIES record)
        {
            if (record.GUID_RECORD == null || record.GUID_RECORD == Guid.Empty)
                EdexContext.AddSupplier(record);
            else
                EdexContext.UpdateSupplier(record);

            return Content("OK");
        }

        public ActionResult Delete(string id)
        {
            EdexContext.DeleteSupplier(id);
            return Content("OK");
        }
    }
}
